﻿Imports Negocio
Imports Servicios
Imports Excepciones

Public Class frmABMTarea
    Private _tarea As New Tarea()
    Private _proyecto As New Proyecto()

    Protected Overrides Sub PreLoad()
        Me.Icon = Icon.FromHandle(Global.SIPRO.My.Resources.Resources.task.GetHicon())
    End Sub

    Sub New(proyecto As Proyecto)
        InitializeComponent()
        _proyecto = proyecto
    End Sub
    Sub New(tarea As Tarea, proyecto As Proyecto)
        Me.New(proyecto)
        _tarea = tarea
        btnGuardar.Tag = "Modificar"
        btnCalcularProgreso.Enabled = True
    End Sub

    Private Sub frmABMTarea_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Init()
        txtTitulo.DataBindings.Add("Text", _tarea, "Titulo")
        txtDescripcion.DataBindings.Add("Text", _tarea, "Descripcion")
        dtFechaInicio.Value = _tarea.FechaInicio
        numHoras.DataBindings.Add("Value", _tarea, "Horas")
        cbPrioridad.DataBindings.Add("SelectedItem", _tarea, "Prioridad")
    End Sub

    Private Sub Init()

        dtFechaInicio.Value = DateTime.Now
        Try
            cbPrioridad.DataSource = System.Enum.GetValues(GetType(Nivel))
        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try

    End Sub
    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        If MyBase.ValidateForm() Then
            Try
                _tarea.FechaInicio = dtFechaInicio.Value
                GesTareas.Guardar(_tarea, _proyecto)
                Me.Close()
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Catch ex As Exception
                My.Application.HandlerException(New TareaSaveException())
            End Try
        End If
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnArchivos_Click(sender As System.Object, e As System.EventArgs) Handles btnArchivos.Click

    End Sub

    Private Sub btnCalcularProgreso_Click(sender As System.Object, e As System.EventArgs) Handles btnCalcularProgreso.Click
        Try
            Dim msg As String = GesIdiomas.TraduceMensaje("MSGTAR002").ToString()
            MessageBox.Show(String.Format(msg, GesTareas.CalcularProgreso(_tarea)))
        Catch ex As Exception
            My.Application.HandlerException(ex)
        End Try
    End Sub
End Class